package com.reden.utils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil {

    // 将对应路径下的文件压缩到指定位置
    public static void pack(String sourcePath, String targetPath, String fileName) throws IOException {
        if (ValidateUtil.isBlank(sourcePath)) throw new NullPointerException("Param 'sourcePath' can't be null or empty.");
        if (ValidateUtil.isBlank(targetPath)) throw new NullPointerException("Param 'targetPath' can't be null or empty.");
        if (ValidateUtil.isBlank(fileName)) throw new NullPointerException("Param 'fileName' can't be null or empty.");
        pack(new File(sourcePath), new File(targetPath), fileName);
    }

    public static void pack(File source, File target, String fileName) throws IOException {
        if (ValidateUtil.isNull(source)) throw new NullPointerException("Param 'source' can't be null.");
        if (ValidateUtil.isNull(target)) throw new NullPointerException("Param 'target' can't be null.");
        if (ValidateUtil.isBlank(fileName)) throw new NullPointerException("Param 'fileName' can't be null or empty.");
        FileOutputStream fos = new FileOutputStream(target.getPath() + "\\" + fileName);
        ZipOutputStream zos = new ZipOutputStream(fos);
        if (!source.exists())
            throw new FileNotFoundException();
        if (!target.exists())
            target.mkdirs();
        byte[] buf = new byte[1024];
        if (source.isDirectory()) {
            for (File file : source.listFiles()) {
                FileInputStream fis = new FileInputStream(file);
                zos.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = fis.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
                fis.close();
            }
        } else {
            FileInputStream fis = new FileInputStream(source);
            zos.putNextEntry(new ZipEntry(source.getName()));
            int len;
            while ((len = fis.read(buf)) > 0) {
                zos.write(buf, 0, len);
            }
            fis.close();
        }
        zos.close();
        fos.close();
    }

    // 读取对应路径的文件内容
    public static String read(String filePath) throws IOException {
        if (ValidateUtil.isBlank(filePath)) throw new NullPointerException("Param 'filePath' can't be null or empty.");
        return read(new File(filePath));
    }

    public static String read(File file) throws IOException {
        if (ValidateUtil.isNull(file)) throw new NullPointerException("Param 'file' can't be null.");
        BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line;
        String result = "";
        while ((line = read.readLine()) != null)
            result += line;
        return result;
    }

    // 在对应路径的文件中写入内容
    public static void write(String filePath, String content) throws IOException {
        if (ValidateUtil.isBlank(filePath)) throw new NullPointerException("Param 'filePath' can't be null or empty.");
        if (ValidateUtil.isNull(content)) throw new NullPointerException("Param 'content' can't be null.");
        write(new File(filePath), content);
    }

    public static void write(File file, String content) throws IOException {
        if (ValidateUtil.isNull(file)) throw new NullPointerException("Param 'file' can't be null.");
        if (ValidateUtil.isNull(content)) throw new NullPointerException("Param 'content' can't be null.");
        FileOutputStream fs = new FileOutputStream(file, true);
        fs.write(content.getBytes());
        fs.flush();
        fs.close();
    }

    // 在对应路径的文件中覆写内容
    public static void rewrite(String filePath, String content) throws IOException {
        if (ValidateUtil.isBlank(filePath)) throw new NullPointerException("Param 'filePath' can't be null or empty.");
        if (ValidateUtil.isNull(content)) throw new NullPointerException("Param 'content' can't be null.");
        rewrite(new File(filePath), content);
    }

    public static void rewrite(File file, String content) throws IOException {
        if (ValidateUtil.isNull(file)) throw new NullPointerException("Param 'file' can't be null.");
        if (ValidateUtil.isNull(content)) throw new NullPointerException("Param 'content' can't be null.");
        FileOutputStream fs = new FileOutputStream(file, false);
        fs.write(content.getBytes());
        fs.flush();
        fs.close();
    }
}
